package pl.edu.agh.gronoMining.frequentSubgraphFinder.frequentSearcher.subgraph.impl;

import java.util.Comparator;

import pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.Edge;
import pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.Node;

/**
 * Comparator that is used to compare two edges using DFS leksographic order
 * @param <T> should be Node
 */
public class EdgeDFSComparator<T extends Edge> implements Comparator<T> {

	private Comparator<Node> nodesComparator;
	
	public EdgeDFSComparator(Comparator<Node> nodesComparator) {
		this.nodesComparator = nodesComparator;
	}
	
	public int compare(T o1, T o2) {
		Edge e1 = (Edge) o1;
		Edge e2 = (Edge) o2;
		
		int c = nodesComparator.compare(e1.getNodeA(), e2.getNodeA());
		if (c < 0)
			return 1;
		if (c > 0)
			return -1;
		
		c = nodesComparator.compare(e1.getNodeB(), e2.getNodeB());
		if (c < 0)
			return 1;
		if (c > 0)
			return -1;
		
		if (e1.getLabel() < e2.getLabel())
			return 1;
		if (e1.getLabel() > e2.getLabel())
			return -1;
		
		return 0;
	}
	
}
